<?php
namespace app\index\controller;

use think\Request;
use app\index\model\NavModel;
use app\index\model\VisitorModel;
class Index extends Base
{
    public function index(){
		$navModel = new NavModel();
		$NavTop = $navModel->where("pid",5)->find()->toArray();
		$NavLeft = $navModel->where("pid",6)->select()->toArray();
		$NavRight = $navModel->where("pid",7)->select()->toArray();
		$this->assign([
			'NavTop' => $NavTop,
			'NavLeft' => $NavLeft,
			'NavRight' => $NavRight
		]);
        return $this->fetch();
    }

	public function details() {
		return $this->fetch();
	}

	public function resume() {
		if($this->request->isPost()){
			$param = input("post.");
			if($param['phone'] == "18388400315" || $param['name'] == "zengyichen"){
				session("is_resume",1);
				return ['code'=>1,'msg'=>'答对了','data'=>'index/index/myresume'];
			}else{
				session("is_resume",0);
				return ['code'=>0,'msg'=>'对不起，您回答错误，不能看我的简历了'];
			}
		}
		return $this->fetch();
	}

	public function myresume() {
		if(session('is_resume') != 1){
			$this->error("对不起，您在回答问题之前不能看我的简历","index/index/resume");
		}
		return $this->fetch();
	}

	public function zyc() {
		return $this->fetch("myresume");
	}

	public function DownloadAPK() {
		$path = APP_PATH."../public/Finalove.apk";
		if (! file_exists ( $path )) {
		    header('HTTP/1.1 404 NOT FOUND');
		} else {
		    //以只读和二进制模式打开文件
		    $file = fopen ( $path, "rb" );
		    //告诉浏览器这是一个文件流格式的文件
		    Header ( "Content-type:application/octet-stream");
		    //请求范围的度量单位
		    Header ( "Accept-Ranges:bytes" );
		    //Content-Length是指定包含于请求或响应中数据的字节长度
		    // Header ( "Accept-Length: " . filesize ( $path ) );
			Header("Content-Length:".filesize($path));
		    //用来告诉浏览器，文件是可以当做附件被下载，下载后的文件名称为$file_name该变量的值。
		    Header ( "Content-Disposition: attachment; filename=Finalove.apk");
		    //读取文件内容并直接输出到浏览器
		    echo fread ( $file, filesize ( $path ) );
		    fclose ( $file );
		    exit ();
		}
	}

	/**
	 * [app端用户头像上传]
	 */
	public function uploadHeadByClient() {
		$user_id = input('post.user_id');
		$visitorModel = new VisitorModel();
		$old_file = $visitorModel->where('id',$user_id)->value('head');
		$old_file = APP_PATH."../public".$old_file;
		$old_file = iconv("UTF-8", "GBK", $old_file);
		if(file_exists($old_file)){
			@unlink($old_file);
		}
		if(!isset($_FILES["Filedata"])||empty($_FILES["Filedata"])){
            return ['code'=>0,'msg'=>'没有上传文件'];
        }else{
			// print_r($_FILES["Filedata"]);die;
			$ext = explode(".",$_FILES["Filedata"]['name'])[1];
			$tmp = $_FILES["Filedata"]['tmp_name'];
			$name = $user_id."_".date("YmdHis");
			$path = "upload/visitor_head";
			$save = $path."/".$name.".".$ext;
			try {
				if (!is_dir($path)) {
					mkdir(iconv("UTF-8", "GBK", $path), 0777, true);
				}
				if (move_uploaded_file($tmp, iconv("UTF-8", "GBK", $save))) {
					$res = $visitorModel->where('id',$user_id)->update(['head'=>"/".$save]);
					if($res !== false){
						$return = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'].$save;
						return json(['code'=>1,'msg'=>'头像修改成功','data'=>$return]);
					}else{
						$default_save = "/static/admin/images/user.png";
						$visitorModel->where('id',$user_id)->update(['head'=>$default_save]);
						return json(['code'=>0,'msg'=>'头像修改失败：无法写入数据库','data'=>'']);
					}
				}
			} catch (\Exception $e) {
				return json(['code'=>0,'msg'=>'头像修改失败:'.$e->getMessage(),'data'=>'']);
			}
		}
	}

	/**
	 * [app端二维码加好友]
	 */
	public function applyByQRcode() {
		$param = input('get.');
		$visitorModel = new VisitorModel();
		$data = $visitorModel::where('id',$param['to_id'])->field("id,user_name,nick_name,email,head")->find();
		if(empty($data)){
			return json(['code'=>0,'msg'=>"没有找到该用户",'data'=>""]);
		}
		$data['head'] = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'].$data['head'];
		return json(['code'=>1,'msg'=>"ok",'data'=>$data]);
	}
}
